package leetcode.day;

import java.util.HashMap;
import java.util.Map;

// 1791. 找出星型图的中心节点
public class FindCenter {

    public static void main(String[] args) {

    }

    // 暴力，找最多的元素
    public int findCenter1(int[][] edges) {

        Map<Integer, Integer> map = new HashMap<>();
        for (int[] edge : edges) {
            for (int j = 0; j < edges[0].length; j++) {
                map.putIfAbsent(edge[j], 0);
                map.put(edge[j], map.get(edge[j]) + 1);
            }
        }
        int max = Integer.MIN_VALUE;
        int res = 0;
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            if (entry.getValue() > max) {
                max = entry.getValue();
                res = entry.getKey();
            }
        }
        return res;

    }

    public int findCenter2(int[][] edges) {

        int ans = edges[0][0];
        if(ans == edges[1][0] || ans == edges[1][1]) {
            return ans;
        } else {
            return edges[0][1];
        }
    }
}
